﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DatabaseConcept
{
    public partial class Form1 : Form
    {
        private Dictionary<string, string> events = new Dictionary<string, string>();
        private Dictionary<string, string> tournamentsEvents = new Dictionary<string, string>();
        private Dictionary<string, string> ticketTypes = new Dictionary<string, string>();
        private Dictionary<string, string> elimTypes = new Dictionary<string, string>();
        private Dictionary<string, string> games = new Dictionary<string, string>();
        private Dictionary<string, string> members = new Dictionary<string, string>();
        private List<string> member_names = new List<string>();     

        public Form1()
        {
            InitializeComponent();
            populateSelectables();
        }

        /// <summary>
        /// Refresh the database list items in the UI
        /// </summary>
        private void populateSelectables()
        {
            Query.Instance.populateTicketSaleView(ticket_gridview);
            
            Query.Instance.populateTeamMembers(members);
            team_member_cmb.DataSource = null;
            team_member_cmb.DisplayMember = "Value";
            team_member_cmb.ValueMember = "Key";
            team_member_cmb.DataSource = new BindingSource(members, null);

            if (members.Keys.Count == 0)
            {
                team_member_cmb.Text = "";
            }
            Query.Instance.populateTeamMembers(members);
            team_member_cmb.DataSource = null;
            team_member_cmb.DisplayMember = "Value";
            team_member_cmb.ValueMember = "Key";
            team_member_cmb.DataSource = new BindingSource(members, null);

            if (members.Keys.Count == 0)
            {
                team_member_cmb.Text = "";
            }


            System.Windows.Forms.ListControl[] usesEvents = new System.Windows.Forms.ListControl[3]{
                 event_lb, tevent_cmb, ticket_event_box 
             };

            Query.Instance.populateEventDisctionary(events);
            foreach (System.Windows.Forms.ListControl c in usesEvents)
            {
                c.DataSource = null;
                c.DisplayMember = "Value";
                c.ValueMember = "Key";
                c.DataSource = new BindingSource(events, null);

                if (events.Keys.Count == 0)
                {
                    event_lb.Text = "";
                }
            }

            if (event_lb.SelectedValue != null)
            {
                Query.Instance.populateTournamentsEventDisctionary(tournamentsEvents, event_lb.SelectedValue.ToString());
                tourn_lb.DataSource = null;
                tourn_lb.DisplayMember = "Value";
                tourn_lb.ValueMember = "Key";
                tourn_lb.DataSource = new BindingSource(tournamentsEvents, null);
                if (tournamentsEvents.Keys.Count == 0)
                {
                    tourn_lb.Text = "";
                }
            }


            if (tourn_lb.SelectedValue != null && event_lb.SelectedValue != null)
            {
                Dictionary<String, String> matchesList = new Dictionary<string, string>();
                Query.Instance.populateMatchesList(matchesList, tourn_lb.SelectedValue.ToString());

                matches_list.DataSource = null;

                matches_list.DisplayMember = "Value";
                matches_list.ValueMember = "Key";
                matches_list.DataSource = new BindingSource(matchesList, null);
            }

            if (tourn_lb.SelectedValue != null && event_lb.SelectedValue != null && matches_list.SelectedValue != null)
            {
                Dictionary<String, String> matchTeams = new Dictionary<string, string>();
                Query.Instance.populateMatchesTeams(matchTeams, tourn_lb.SelectedValue.ToString(), matches_list.SelectedValue.ToString());

                matches_winner_cb.DataSource = null;
                matches_winner_cb.DisplayMember = "Value";
                matches_winner_cb.ValueMember = "Key";
                matches_winner_cb.DataSource = new BindingSource(matchTeams, null);
            }


            Query.Instance.populateTicketTypes(ticketTypes);
            ticket_type_box.DataSource = null;

            ticket_type_box.DisplayMember = "Value";
            ticket_type_box.ValueMember = "Key";
            ticket_type_box.DataSource = new BindingSource(ticketTypes, null);
            if (ticketTypes.Keys.Count == 0)
            {
                ticket_type_box.Text = "";
            }


            Query.Instance.populateElimTypes(elimTypes);
            eliminationType_cmb.DataSource = null;

            eliminationType_cmb.DisplayMember = "Value";
            eliminationType_cmb.ValueMember = "Key";
            eliminationType_cmb.DataSource = new BindingSource(elimTypes, null);
            if (elimTypes.Keys.Count == 0)
            {
                eliminationType_cmb.Text = "";
            }

            Dictionary<string, string> teams = new Dictionary<string, string>();
            if (tourn_lb.SelectedValue != null && event_lb.SelectedValue != null)
            {
                Query.Instance.populateTeams(teams, tourn_lb.SelectedValue.ToString());
                match_team1_cmb.DataSource = null;
                match_team2_cmb.DataSource = null;
                teams_lb.DataSource = null;

                teams_lb.DisplayMember = "Value";
                teams_lb.ValueMember = "Key";
                teams_lb.DataSource = new BindingSource(teams, null);

                match_team1_cmb.DisplayMember = "Value";
                match_team1_cmb.ValueMember = "Key";
                match_team1_cmb.DataSource = new BindingSource(teams, null);

                match_team2_cmb.DisplayMember = "Value";
                match_team2_cmb.ValueMember = "Key";
                match_team2_cmb.DataSource = new BindingSource(teams, null);
                if (teams.Keys.Count == 0)
                {
                    match_team1_cmb.Text = "";
                    match_team2_cmb.Text = "";
                }
            }


            Query.Instance.populateGames(games);
            game_lb.DataSource = null;
            tourn_game_cmb.DataSource = null;

            game_lb.DisplayMember = "Value";
            game_lb.ValueMember = "Key";
            game_lb.DataSource = new BindingSource(games, null);

            tourn_game_cmb.DisplayMember = "Value";
            tourn_game_cmb.ValueMember = "Key";
            tourn_game_cmb.DataSource = new BindingSource(games, null);
            if (games.Keys.Count == 0)
            {
                game_lb.Text = "";
                tourn_game_cmb.Text = "";
            }

        }

        /// <summary>
        /// Event list selection change listener
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void event_lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (event_lb.SelectedValue != null)
            {
                Query.Instance.populateTournamentsEventDisctionary(tournamentsEvents, event_lb.SelectedValue.ToString());
                tourn_lb.DataSource = null;

                tourn_lb.DisplayMember = "Value";
                tourn_lb.ValueMember = "Key";
                tourn_lb.DataSource = new BindingSource(tournamentsEvents, null);
            }
        }

        /// <summary>
        /// Tournament list selection change listener
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tourn_lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tourn_lb.SelectedValue != null && event_lb.SelectedValue != null)
            {
                Dictionary<String, String> matchesList = new Dictionary<string, string>();
                Query.Instance.populateMatchesList(matchesList, tourn_lb.SelectedValue.ToString());

                matches_list.DataSource = null;

                matches_list.DisplayMember = "Value";
                matches_list.ValueMember = "Key";
                matches_list.DataSource = new BindingSource(matchesList, null);

                Dictionary<string, string> teams = new Dictionary<string, string>();
                Query.Instance.populateTeams(teams, tourn_lb.SelectedValue.ToString());
                match_team1_cmb.DataSource = null;
                match_team2_cmb.DataSource = null;
                teams_lb.DataSource = null;

                teams_lb.DisplayMember = "Value";
                teams_lb.ValueMember = "Key";
                teams_lb.DataSource = new BindingSource(teams, null);

                match_team1_cmb.DisplayMember = "Value";
                match_team1_cmb.ValueMember = "Key";
                match_team1_cmb.DataSource = new BindingSource(teams, null);

                match_team2_cmb.DisplayMember = "Value";
                match_team2_cmb.ValueMember = "Key";
                match_team2_cmb.DataSource = new BindingSource(teams, null);
                if (teams.Keys.Count == 0)
                {
                    match_team1_cmb.Text = "";
                    match_team2_cmb.Text = "";
                }
            }
        }

        /// <summary>
        /// Matches list selection change listener
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void matches_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tourn_lb.SelectedValue != null && event_lb.SelectedValue != null && matches_list.SelectedValue != null)
            {
                Dictionary<String, String> matchTeams = new Dictionary<string, string>();
                Query.Instance.populateMatchesTeams(matchTeams,  tourn_lb.SelectedValue.ToString(), matches_list.SelectedValue.ToString());

                matches_winner_cb.DataSource = null;
                matches_winner_cb.DisplayMember = "Value";
                matches_winner_cb.ValueMember = "Key";
                matches_winner_cb.DataSource = new BindingSource(matchTeams, null);
            }
        }

        /// <summary>
        /// Create tournament button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tourn_btn_Click(object sender, EventArgs e)
        {
            // string name, string prize, string game, int elimType
            Query.Instance.CreateTournament(tname_txt.Text, prize_txt.Text, tourn_game_cmb.SelectedValue.ToString(),
                eliminationType_cmb.SelectedValue.ToString(), tevent_cmb.SelectedValue.ToString(), tourn_maxTeams_cmb.SelectedText);
            tname_txt.Text = "";
            prize_txt.Text = "";

            populateSelectables();
        }

        /// <summary>
        /// create event button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void event_btn_Click(object sender, EventArgs e)
        {
            Query.Instance.CreateEvent(ename_txt.Text, eCalendar.SelectionStart.ToShortDateString(), eplace_txt.Text);
            ename_txt.Text = "";
            eCalendar.SelectionStart = DateTime.Now;
            eCalendar.SelectionEnd = DateTime.Now;
            eplace_txt.Text = "";

            populateSelectables();
        }

        /// <summary>
        /// Create ticket button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ticket_btn_Click(object sender, EventArgs e)
        {
            Query.Instance.CreateTicket(name_box.Text, ticket_event_box.SelectedValue.ToString(),
                    ticket_type_box.SelectedValue.ToString());
            name_box.Text = "";
            populateSelectables();
        }

        /// <summary>
        /// Create member button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void member_btn_Click(object sender, EventArgs e)
        {
            Query.Instance.CreateMember(member_name_box.Text, member_email_box.Text,
                                        member_password_label.Text);
            member_name_box.Text = "";
            member_email_box.Text = "";
            member_password_box.Text = "";
            populateSelectables();
        }

        /// <summary>
        /// Add game button was click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameAdd_btn_Click(object sender, EventArgs e)
        {
            Query.Instance.CrateGame(gameName_txt.Text, gameDesc_txt.Text);
            gameName_txt.Text = "";
            gameDesc_txt.Text = "";
            populateSelectables();
        }

        /// <summary>
        /// Remove game button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameRemove_Btn_Click(object sender, EventArgs e)
        {
            Query.Instance.RemoveGame(game_lb.SelectedValue.ToString());
            populateSelectables();
        }

        /// <summary>
        /// add match button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void match_add_Click(object sender, EventArgs e)
        {
            Query.Instance.addMatch(event_lb.SelectedValue.ToString(), tourn_lb.SelectedValue.ToString(), 
                match_team1_cmb.SelectedValue.ToString(), match_team2_cmb.SelectedValue.ToString());
            populateSelectables();
        }

        /// <summary>
        /// Selected tab has changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabController_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Move common lists to the selected tab
            TabPage curTab = TabController.SelectedTab;
            if (curTab == teamTab || curTab == eventTabPage || curTab == addMatchTabPage || curTab == matchTabPage)
            {
                TabController.SelectedTab.Controls.Add(this.event_lbl);
                TabController.SelectedTab.Controls.Add(this.tourn_lbl);
                TabController.SelectedTab.Controls.Add(this.tourn_lb);
                TabController.SelectedTab.Controls.Add(this.event_lb);
            }
        }

        /// <summary>
        /// Update the match with the winner
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void match_update_Click(object sender, EventArgs e)
        {

            Query.Instance.UpdateMatchWinner(matches_list.SelectedValue.ToString(), matches_winner_cb.SelectedValue.ToString());
            // call populate selectables
            populateSelectables();
        }

        /// <summary>
        /// add member to team list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_member_btn_Click(object sender, EventArgs e)
        {
            member_names.Add(team_member_cmb.SelectedValue.ToString());
        }

        /// <summary>
        /// Add new team button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void make_team_btn_Click(object sender, EventArgs e)
        {
            // query create team
            Query.Instance.CreateTeam(team_name_box.Text, member_names);
            // call populate selectables
            populateSelectables();
            // clear fields
            team_name_box.Text = "";
            team_member_cmb.Text = "";
            member_names.Clear();
        }

        /// <summary>
        /// Team wants to participate in Tournament
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void team_InTourn_Click(object sender, EventArgs e)
        {
            // query create team
            Query.Instance.AddTeamToTournament(tourn_lb.SelectedValue.ToString(), teams_lb.SelectedValue.ToString());
            // call populate selectables
            populateSelectables();
        }
    }
}
